package com.stone.business.tabl.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.stone.business.tabl.pojo.TTablJk;
import com.stone.business.tabl.service.TablService;
import com.stone.common.pojo.TFile;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TUser;
import com.stone.utils.DateFormatTool;
import com.stone.utils.FileUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class TablJkAction extends BaseAction {
	private static final long serialVersionUID = -8092652494626277829L;
	private TablService tablService;
	private TTablJk entity;
	
	private String id;
	private String jksd;
	private File myFile1;
	private String myFile1FileName;
	private File myFile2;
	private String myFile2FileName;
	private File myFile3;
	private String myFile3FileName;
	
	private List<TFile> xsbFileList;
	public TablService getTablService() {
		return tablService;
	}

	public void setTablService(TablService tablService) {
		this.tablService = tablService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getJksd() {
		return jksd;
	}

	public void setJksd(String jksd) {
		this.jksd = jksd;
	}

	public TTablJk getEntity() {
		return entity;
	}

	public void setEntity(TTablJk entity) {
		this.entity = entity;
	}

	public File getmyFile1() {
		return myFile1;
	}

	public void setmyFile1(File myFile1) {
		this.myFile1 = myFile1;
	}

	public List<TFile> getXsbFileList() {
		return xsbFileList;
	}

	public void setXsbFileList(List<TFile> xsbFileList) {
		this.xsbFileList = xsbFileList;
	}

	public String getmyFile1FileName() {
		return myFile1FileName;
	}

	public void setmyFile1FileName(String myFile1FileName) {
		this.myFile1FileName = myFile1FileName;
	}

	public File getMyFile1() {
		return myFile1;
	}

	public void setMyFile1(File myFile1) {
		this.myFile1 = myFile1;
	}

	public String getMyFile1FileName() {
		return myFile1FileName;
	}

	public void setMyFile1FileName(String myFile1FileName) {
		this.myFile1FileName = myFile1FileName;
	}

	public File getMyFile2() {
		return myFile2;
	}

	public void setMyFile2(File myFile2) {
		this.myFile2 = myFile2;
	}

	public String getMyFile2FileName() {
		return myFile2FileName;
	}

	public void setMyFile2FileName(String myFile2FileName) {
		this.myFile2FileName = myFile2FileName;
	}

	public File getMyFile3() {
		return myFile3;
	}

	public void setMyFile3(File myFile3) {
		this.myFile3 = myFile3;
	}

	public String getMyFile3FileName() {
		return myFile3FileName;
	}

	public void setMyFile3FileName(String myFile3FileName) {
		this.myFile3FileName = myFile3FileName;
	}

	public String initTablJkDetail() throws AppException{
		// 修改进入
		if (!"0".equals(id.toString())) {
			this.entity = (TTablJk) tablService.findById(Long.parseLong(id),
					TTablJk.class.getName());
			getFileList();
			// 新建进入
		} else {
			this.entity = new TTablJk();
			String sysdate = tablService.getSysdate("yyyy-MM-dd");
			this.entity.setStartdate(DateFormatTool.formatToDate(sysdate, false));
			this.entity.setStatus("00");
			TUser loginUser  = (TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);//系统当前登录用户
			this.entity.setFqr(loginUser.getName());
		}
		super.getRequest().setAttribute("currentStatus", entity.getStatus());
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public void getFileList() throws AppException {
		xsbFileList = tablService.findListByHql("from TFile where tableid = '" + id + "' AND colname = 'TAB_TABL_JK'  order by id");
	}
	
	public String addTablJk() throws AppException{
		if (myFile1 != null && (myFile1.length() > IConstant.FILE_BUFFER_SIZE
						* IConstant.FILE_SIZE_TEN * IConstant.FILE_SIZE_HUNDRED)) {
			super.setInfo("上传失败，附件不能超过10M");//OUTOFSIZE
			return SUCCESS;
		}
		ArrayList<TFile> fileList = new ArrayList<TFile>();
		TFile tFile = null;
		if(myFile1!=null){
			System.out.println("==========" + myFile1FileName);
			String filePath = super.getRealPath() + "upload/tabl_jk_files";
            
            File upDir = new File(filePath);
            if (!upDir.exists())
            {
                upDir.mkdirs();
            }
            
            String saveFileName = UUID.randomUUID().toString() + FileUtils.getExtention(myFile1FileName);
            File saveFile = new File(filePath + "\\" +saveFileName);
         // 上传文件
			try {
				org.apache.commons.io.FileUtils.copyFile(myFile1, saveFile);
				tFile = new TFile();
				tFile.setUrl("/DLPLANMIS/upload/tabl_jk_files/" + saveFileName);
				tFile.setFilename("监控信息设计");
				tFile.setRealfilename(saveFileName);
				
				tFile.setTableid(entity.getId());
				tFile.setColname("TAB_TABL_JK");
				tFile.setOperuser(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				tFile.setOperdate(DateFormatTool.formatToDate(tablService
						.getSysdate("yyyy-MM-dd HH24:mi"), true));
				tFile.setImageflag("0");
				
				fileList.add(tFile);
			} catch (IOException e) {
				e.printStackTrace();
				super.setInfo("文件上传出现错误");
				throw new AppException("上传失败");
			}
		}
		String rtn = tablService.doJkSave(entity,  fileList);
		super.setInfo(rtn);
		this.setId(entity.getId() + "");
		getFileList();
		super.getRequest().setAttribute("currentStatus", entity.getStatus());
		return SUCCESS;
	}
	
	public String updateTablJk() throws AppException{
//		if (myFile1 != null && (myFile1.length() > IConstant.FILE_BUFFER_SIZE
//						* IConstant.FILE_SIZE_TEN * IConstant.FILE_SIZE_HUNDRED)) {
//			super.setInfo("上传失败，附件不能超过10M");//OUTOFSIZE
//			return SUCCESS;
//		}
		ArrayList<TFile> fileList = new ArrayList<TFile>();
		TFile tFile1 = null;
		TFile tFile2 = null;
		TFile tFile3 = null;
		String filePath = super.getRealPath() + "upload/tabl_jk_files";
        
        File upDir = new File(filePath);
        if (!upDir.exists()){
            upDir.mkdirs();
        }
        
		String nextStatus = entity.getStatus();
		TUser loginUser  = (TUser)super.getSession().getAttribute(IConstant.SESSION_TUSER);//系统当前登录用户
		String sysdate = tablService.getSysdate("yyyy-MM-dd");
		if("02".equals(nextStatus)){
			this.entity.setZlr(loginUser.getName());
			this.entity.setZlrq(DateFormatTool.formatToDate(sysdate, false));
			
			//检查是否之前上传过定值计算, 如果有的话，先删除历史文件
			String sql = "from TFile where tableid = '" + this.entity.getId() + "' AND colname = 'TAB_TABL_JK' AND filename='监控信息顺序表'";
			TFile tmpFile = (TFile)tablService.findFirst(sql);
			if(tmpFile != null){
				File f = new File(filePath + "\\" + tmpFile.getRealfilename());
				if(f.exists())
					f.delete();
				tablService.delete(tmpFile.getId(), TFile.class.getName());
			}
		}else if("03".equals(nextStatus)){
			this.entity.setTbr(loginUser.getName());
			this.entity.setTbrq(DateFormatTool.formatToDate(sysdate, false));
			//检查是否之前上传过主站监控信息, 如果有的话，先删除历史文件
			String sql = "from TFile where tableid = '" + this.entity.getId() + "' AND colname = 'TAB_TABL_JK' AND filename='主站监控信息'";
			TFile tmpFile = (TFile)tablService.findFirst(sql);
			if(tmpFile != null){
				File f = new File(filePath + "\\" + tmpFile.getRealfilename());
				if(f.exists())
					f.delete();
				tablService.delete(tmpFile.getId(), TFile.class.getName());
			}
		}else if("05".equals(nextStatus)){
			this.entity.setSdr(loginUser.getName());
			this.entity.setSdrq(DateFormatTool.formatToDate(sysdate, false));
			
			if("N".equals(this.getJksd()))
				this.entity.setStatus("05");
		}else if("99".equals(nextStatus)){
			this.entity.setAztsr(loginUser.getName());
			this.entity.setAztsrq(DateFormatTool.formatToDate(sysdate, false));
			
			if("N".equals(this.entity.getYwyc()) && "N".equals(this.entity.getYwwt()))
				this.entity.setStatus("11");	//无异常、无问题，准备归档
			else if("Y".equals(this.entity.getYwyc()))
				this.entity.setStatus("06");
			else
				this.entity.setStatus("08");
		}else if("07".equals(nextStatus)){
			this.entity.setHdr(loginUser.getName());
			this.entity.setHdrq(DateFormatTool.formatToDate(sysdate, false));
		}else if("98".equals(nextStatus)){
			this.entity.setXgr(loginUser.getName());
			this.entity.setXgrq(DateFormatTool.formatToDate(sysdate, false));
			
			this.entity.setStatus("11");
		}else if("97".equals(nextStatus)){
			this.entity.setWtqrr(loginUser.getName());
			this.entity.setWtqrrq(DateFormatTool.formatToDate(sysdate, false));
			
			if("N".equals(this.getJksd()))
				this.entity.setStatus("11");
			else
				this.entity.setStatus("09");
		}else if("10".equals(nextStatus)){
			this.entity.setXxxgr(loginUser.getName());
			this.entity.setXxxgrq(DateFormatTool.formatToDate(sysdate, false));
		}else if("96".equals(nextStatus)){
			this.entity.setSdr2(loginUser.getName());
			this.entity.setSdrq2(DateFormatTool.formatToDate(sysdate, false));
			
			if("N".equals(this.getJksd()))
				this.entity.setStatus("11");
			else
				this.entity.setStatus("07");
		}
		
		if(myFile1!=null){
			System.out.println("==========" + myFile1FileName);
            
            String saveFileName = UUID.randomUUID().toString() + FileUtils.getExtention(myFile1FileName);
            File saveFile = new File(filePath + "\\" +saveFileName);
         // 上传文件
			try {
				org.apache.commons.io.FileUtils.copyFile(myFile1, saveFile);
				tFile1 = new TFile();
				tFile1.setUrl("/DLPLANMIS/upload/tabl_jk_files/" + saveFileName);
				if("02".equals(nextStatus))
					tFile1.setFilename("监控信息顺序表");
				else if("03".equals(nextStatus))
					tFile1.setFilename("主站监控信息");
				else if("99".equals(nextStatus))
					tFile1.setFilename("监控信息设备安装");
				else if("07".equals(nextStatus))
					tFile1.setFilename("主站监控信息核对");
				else if("98".equals(nextStatus))
					tFile1.setFilename("变电站监控系统修改报告");
				else if("97".equals(nextStatus))
					tFile1.setFilename("问题确认报告");
				else if("97".equals(nextStatus))
					tFile1.setFilename("主站监控信息修改报告");
				tFile1.setRealfilename(saveFileName);
				tFile1.setTableid(entity.getId());
				tFile1.setColname("TAB_TABL_JK");
				tFile1.setOperuser(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				tFile1.setOperdate(DateFormatTool.formatToDate(tablService
						.getSysdate("yyyy-MM-dd HH24:mi"), true));
				tFile1.setImageflag("0");
				fileList.add(tFile1);
			} catch (IOException e) {
				e.printStackTrace();
				super.setInfo("文件上传出现错误");
				throw new AppException("上传失败");
			}
		}
		if(myFile2!=null){
			System.out.println("==========" + myFile2FileName);
            
            String saveFileName = UUID.randomUUID().toString() + FileUtils.getExtention(myFile2FileName);
            File saveFile = new File(filePath + "\\" +saveFileName);
         // 上传文件
			try {
				org.apache.commons.io.FileUtils.copyFile(myFile2, saveFile);
				tFile2 = new TFile();
				tFile2.setUrl("/DLPLANMIS/upload/tabl_jk_files/" + saveFileName);
				if("99".equals(nextStatus))
					tFile2.setFilename("监控信息设备调试");
				
				tFile2.setRealfilename(saveFileName);
				tFile2.setTableid(entity.getId());
				tFile2.setColname("TAB_TABL_JK");
				tFile2.setOperuser(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				tFile2.setOperdate(DateFormatTool.formatToDate(tablService
						.getSysdate("yyyy-MM-dd HH24:mi"), true));
				tFile2.setImageflag("0");
				fileList.add(tFile2);
			} catch (IOException e) {
				e.printStackTrace();
				super.setInfo("文件上传出现错误");
				throw new AppException("上传失败");
			}
		}
		if(myFile3!=null){
			System.out.println("==========" + myFile3FileName);
            
            String saveFileName = UUID.randomUUID().toString() + FileUtils.getExtention(myFile3FileName);
            File saveFile = new File(filePath + "\\" +saveFileName);
         // 上传文件
			try {
				org.apache.commons.io.FileUtils.copyFile(myFile3, saveFile);
				tFile3 = new TFile();
				tFile3.setUrl("/DLPLANMIS/upload/tabl_jk_files/" + saveFileName);
				if("99".equals(nextStatus))
					tFile3.setFilename("监控信息设备验收");
				tFile3.setRealfilename(saveFileName);
				tFile3.setTableid(entity.getId());
				tFile3.setColname("TAB_TABL_JK");
				tFile3.setOperuser(((TUser) super.getSession().getAttribute(
						IConstant.SESSION_TUSER)).getId());
				tFile3.setOperdate(DateFormatTool.formatToDate(tablService
						.getSysdate("yyyy-MM-dd HH24:mi"), true));
				tFile3.setImageflag("0");
				fileList.add(tFile3);
			} catch (IOException e) {
				e.printStackTrace();
				super.setInfo("文件上传出现错误");
				throw new AppException("上传失败");
			}
		}
		String rtn = tablService.doJkSave(entity,  fileList);
		super.setInfo(rtn);
		this.setId(entity.getId() + "");
		getFileList();
		super.getRequest().setAttribute("currentStatus", entity.getStatus());
		return SUCCESS;
	}
}
